package com.wx.user.controller;



import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.commons.entity.Org;
import com.wx.commons.entity.UserOrg;
import com.wx.user.service.OrgService;
import com.wx.user.service.UserOrgService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
  * (user_org)表控制层
  * @author liuwenxuan
  * @TableName user_org
  * @createDate 20250801.01
  */
@RestController
@RequestMapping("userOrg")
public class UserOrgController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserOrgService userOrgService;

    @Autowired
    private OrgService orgService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param userOrg 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<UserOrg> page, UserOrg userOrg) {
        return success(this.userOrgService.page(page, new QueryWrapper<>(userOrg)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.userOrgService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param userOrg 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody UserOrg userOrg) {
        return success(this.userOrgService.save(userOrg));
    }

    /**
     * 修改数据
     *
     * @param userOrg 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody UserOrg userOrg) {
        return success(this.userOrgService.updateById(userOrg));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.userOrgService.removeByIds(idList));
    }

    @PostMapping("/batch")
     public R batch(@RequestBody List<UserOrg> userOrgs) {
        if (CollectionUtil.isEmpty(userOrgs)){
            return success(true);
        }
        Long orgId = userOrgs.get(0).getOrgId();
        userOrgService.remove(new QueryWrapper<UserOrg>().eq("org_id", orgId));
        return success(this.userOrgService.saveOrUpdateBatch(userOrgs));
    }

    @GetMapping("/getBfsData")
     public R getBfsData(UserOrg userOrg) {
        List<Org> zOrgs = new ArrayList<>();
        List<Org> orgs = orgService.list();
        zOrgs.addAll(orgs.stream().filter(org -> org.getId().equals(userOrg.getOrgId())).collect(Collectors.toList()));
        List<Long> pOrgIds = Arrays.asList(userOrg.getOrgId());
        bfs(zOrgs,orgs,pOrgIds);
        List<Long> orgIds = zOrgs.stream().map(Org::getId).collect(Collectors.toList());
        List<UserOrg> userOrgs = userOrgService.list(new QueryWrapper<UserOrg>().in("org_id", orgIds));
        return success(userOrgs);
    }

    private void bfs(List<Org> zOrgs, List<Org> orgs, List<Long> pOrgIds) {
        if (pOrgIds.size()==0) return;
        List<Org> curOrgs = orgs.stream().filter(org -> pOrgIds.contains(org.getPid())).collect(Collectors.toList());
        zOrgs.addAll(curOrgs);
        bfs(zOrgs,orgs,curOrgs.stream().map(Org::getId).collect(Collectors.toList()));
    }
}

